package fhv.at.imageprocessing.thresholdImage;

import java.io.Serializable;

import javax.media.jai.PlanarImage;

import fhv.at.imageprocessing.ImageChangedEvent;
import fhv.at.imageprocessing.ImageChangedListener;
import fhv.at.imageprocessing.ImageHelper;
import fhv.at.imageprocessing.ImageNeededEvent;
import fhv.at.imageprocessing.ImageProcessor;

/**
 * Threshold image bean
 * 
 * @author phru
 * 
 */
public class ThresholdImage implements ImageChangedListener, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final ImageHelper imageHelper;
	private PlanarImage planarImage;

	public ThresholdImage() {
		int maxParents = 1;
		imageHelper = new ImageHelper(maxParents, this);
	}

	double[] low = new double[1];
	double[] high = new double[1];
	double[] map = new double[1];

	public double getLow() {
		return low[0];
	}

	public void setLow(double low) throws Exception {
		if ((low < 0) || (low > 255)) {
			throw new Exception("Wert muss zwischen 0 und 155 sein");
		} else {
			this.low[0] = low;
			imageHelper.fireImageNeededEvent();
		}
	}

	public double getHigh() {
		return high[0];
	}

	public void setHigh(double high) throws Exception {
		if ((high < 0) || (high > 255)) {
			throw new Exception("Wert muss zwischen 0 und 155 sein");
		} else {
			this.high[0] = high;
			imageHelper.fireImageNeededEvent();
		}
	}

	public double getMap() {
		return map[0];
	}

	public void setMap(double map) throws Exception {
		if ((map < 0) || (map > 255)) {
			throw new Exception("Wert muss zwischen 0 und 155 sein");
		} else {
			this.map[0] = map;
			imageHelper.fireImageNeededEvent();
		}
		imageHelper.fireImageNeededEvent();
	}

	/* Eigentliche Bildbearbeitung */
	@Override
	public void manipulateImage() {
		if (planarImage != null) {
			ImageProcessor ip = ImageProcessor.getInstance();
			planarImage = ip.getThreshold(planarImage, low, high, map);
			imageHelper.fireImageChangedEvent();
		}
	}

	/* Implementation fur Eventregistrierung */
	@Override
	public void addImageChangedListener(ImageChangedListener icl) {
		imageHelper.addImageChangedListener(icl);
	}

	@Override
	public void removeImageChangedListener(ImageChangedListener icl) {
		imageHelper.removeImageChangedListener(icl);
	}

	@Override
	public void addImageNeededListener(ImageChangedListener icl) {
		imageHelper.addImageNeededListener(icl);
	}

	@Override
	public void removeImageNeededListener(ImageChangedListener icl) {
		imageHelper.removeImageNeededListener(icl);
	}

	/* Implementation fur ImageChangedListener */
	@Override
	public void imageValueChanged(ImageChangedEvent ice) {
		System.out.println(getClass().getSimpleName() + " imageValueChanged");
		planarImage = imageHelper.getPlanarImage(ice);
		manipulateImage();
	}

	@Override
	public void imageNeeded(ImageNeededEvent ice) {
		System.out.println(getClass().getSimpleName() + " imageNeeded");
		imageHelper.fireImageChangedEvent();
		// imageHelper.fireImageChangedEvent(ice);
	}

	@Override
	public ImageChangedListener getRefFromSource() {
		return this;
	}

	@Override
	public PlanarImage getPlanarImage() {
		return planarImage;
	}

}
